home *** CD-ROM | disk | FTP | other *** search
- /* sflnme.cl - set file name from string and default.
- (C) Copyright 1983 Gregory R. Mansfield - All Rights Reserved.
- G. R. Mansfield. 83/12/03.
- Ver 1.1-5517.
- */
-
- #include "defstd.h"
- #include "ctype.h"
-
- #define L_DEV 3
- #define L_NAME 8
- #define L_TYP 3
-
- /* local functions */
- static void cpyfld(), pflnme();
-
- sflnme(n, s, d) /* set file name n to s OR d */
- char *n, *s, *d;
- /* Set file name n to s. If device, name, or type fields in s are not
- specified, set them to the corresponding fields of d (the default
- name).
- */
- {
- char dev[L_DEV+1];
- char nme[L_NAME+1];
- char typ[L_TYP+1];
- char *p;
-
- *dev = *nme = *typ = 0;
- dev[L_DEV] = nme[L_NAME] = typ[L_TYP] = 0;
- pflnme(d, dev, nme, typ);
- if (*s) {
- if (strlen(dev) > 1)
- *dev = 0;
- pflnme(s, dev, nme, typ);
- }
- /* Merge device, name and type to name string with ':' and '.' */
- if (*dev) {
- for (p = dev; *p; p++)
- *n++ = toupper(*p);
- *n++ = ':';
- }
- if (strlen(dev) < 2) {
- for (p = nme; *p; p++)
- *n++ = toupper(*p);
- if (*typ) {
- *n++ = '.';
- for (p = typ; *p; p++)
- *n++ = tolower(*p);
- }
- }
- *n = 0;
- }
-
- static void pflnme(s, dev, nme, typ) /* parse file name */
- char *s, *dev, *nme, *typ;
-
- /* Extract device, name, and type from string to dev, name, typ.
- If device or type are missing, dev or typ are unchanged.
- If device or type are blank, dev or typ are set empty.
- If name is blank, name is not changed.
- */
- {
- char *p;
- int i;
-
- if ((p = index(s, ':')) != NULL) { /* process device. */
- if (i = p - s)
- cpyfld(dev, s, min(i, L_DEV));
- else
- *dev = '\0';
- s += ++i;
- }
-
- if ((p = index(s, '.')) == NULL) /* process name */
- i = strlen(s);
- else
- i = p - s;
- if (i)
- cpyfld(nme, s, min(i, L_NAME));
-
- if (p != NULL) { /* process type. */
- ++p;
- if ((i = min(strlen(p), L_TYP)) > 0)
- cpyfld(typ, p, i);
- else
- *typ = '\0';
- }
- }
-
- static void cpyfld(s1, s2, n) /* copy exactly n characters from s2 to s1 */
- char *s1, *s2;
- int n;
- {
- char *p;
- p = s1;
- while (n-- && (*p++ = *s2++))
- ;
- *p++ = '\0';
- }
-